package imaa.escola.professor.service;

import imaa.escola.aluno.datatransfer.AlunoDto;
import imaa.escola.professor.datatransfer.ProfessorDto;
import imaa.escola.professor.domain.Professor;
import imaa.escola.professor.repository.ProfessorRepository;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/professor")
@Controller
public class ProfessorService {
	
	private static final Logger logger = Logger.getLogger(ProfessorService.class);
	
	@Autowired
	 ProfessorRepository professorRepository;
		
	@ResponseBody
	@RequestMapping("/save")
	public ProfessorDto salvar(@RequestBody ProfessorDto dto) {
		
		ProfessorDto result = new ProfessorDto(professorRepository.save(dto.convertToEntity()));
		
		logger.info("Professor inserido na base de dados. ID: " + result.id);
		
		return result;
	}
	@ResponseBody
	@RequestMapping(value = "/get/{idProfessor}", method = RequestMethod.GET, produces = "application/json")
	public ProfessorDto getProfessor(@PathVariable Long idProfessor) {
		return new ProfessorDto(professorRepository.findOne(idProfessor));
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public List<ProfessorDto> listarProfessores() {
		List<Professor> lista = professorRepository.findAll();
		List<ProfessorDto> listaDto = new ArrayList<ProfessorDto>();
		for (Professor professor : lista) {
			ProfessorDto dto = new ProfessorDto(professor);
			listaDto.add(dto);
		}
		return listaDto;
	}
	
	@ResponseBody
	@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")
	public List<ProfessorDto> pesquisarProfessores(@RequestParam(value = "nome") String dto) {
		List<Professor> lista = professorRepository.findByName(dto);
		List<ProfessorDto> listaDto = new ArrayList<ProfessorDto>();
		for (Professor professor : lista) {
			ProfessorDto professorDto = new ProfessorDto(professor);
			listaDto.add(professorDto);
		}
		return listaDto;
	}
	
	@RequestMapping(value = "/advancedSearch", method = RequestMethod.POST)
	public @ResponseBody List<ProfessorDto> pesquisaAvancada(@RequestBody AlunoDto dto) {
		List<Professor> lista = professorRepository.findByName(dto.nome);
		List<ProfessorDto> listaDto = new ArrayList<ProfessorDto>();
		for (Professor aluno : lista) {
			ProfessorDto professorDto = new ProfessorDto(aluno);
			listaDto.add(professorDto);
		}
		return listaDto;
	}
	
	@ResponseBody
	@RequestMapping("/remove/{idProfessor}")
	public void removeProfessor(@PathVariable Long idProfessor) {
		
	 professorRepository.delete(idProfessor);
		
		logger.info("Professor removido na base de dados. ID: " +idProfessor);
		
	}
}

